//
//  CreateFileforOBJC.m
//  ModelCreate
//
//  Created by daibaisheng on 2019/4/23.
//  Copyright © 2019 Mr.Lee. All rights reserved.
//

#import "CreateFileforOBJC.h"

@interface CreateFileforOBJC ()


@property (nonatomic, strong) NSDictionary *nodeModelStringsPlist;

@property (nonatomic, strong) NSString     *modelHeaderFileString;
@property (nonatomic, strong) NSString     *modelMFileString;

@property (nonatomic,strong) NSString *filePah;



@end


@implementation CreateFileforOBJC

- (instancetype)initCreateFileforOBJC:(NSString *)filePah plist:(NSDictionary *)plistDic;
{
    
    if (self = [super init]) {
        
        self.nodeModelStringsPlist = plistDic;
        self.modelHeaderFileString = self.nodeModelStringsPlist[@"modelHeaderFileString"];
        self.modelMFileString      = self.nodeModelStringsPlist[@"modelMFileString"];

        
        self.filePah = filePah;
    }
    
    return self;
}






-(void)creatNeedFile:(NSString *)fileName;
{
    NSString *string = nil;
    NSString *superString = nil;
    NSString *importModel = nil;
    NSString *centent = nil;
    NSString *interface = nil;
    NSString *property = nil;
    
    
    for (int i = 0; i < 3; i++) {
        if (i == 0) {
            string =[NSString stringWithFormat:@"MW%@View",fileName];
            superString = @"MWBaseView";
            importModel =@"\"MWBaseView.h\"";
            centent = @"-(void)injected;\n{\n\n}";
            interface =@"-(void)injected;";
            property = @"";
        }else if(i == 1)
        {
            string =[NSString stringWithFormat:@"MW%@Controller",fileName];
            superString = @"MWBaseViewController";
            
            NSString *importStr = [NSString stringWithFormat:@"#import \"MW%@View.h\"",fileName];
            importModel =[NSString stringWithFormat:@"\"MWBaseViewController.h\"\n%@",importStr];
            centent = [NSString stringWithFormat:@"-(void)viewDidLoad;\n{\n     [super viewDidLoad];\n\n\n      [self injected];\n}\n\n\n#pragma mark-代码注入调试\n-(void)injected{\n    self.%@View =[[MW%@View alloc]init];\n  [self.view addSubview:self.%@View];\n   [self.%@View injected]; \n}",fileName,fileName,fileName,fileName];
            interface =@"";
            property = [NSString stringWithFormat:@"@property(nonatomic,strong)MW%@View *%@View;",fileName,fileName];
        }else
        {
            string =[NSString stringWithFormat:@"MW%@ViewModel",fileName];
            superString = @"NSObject";
            importModel =@"<Foundation/Foundation.h>";
            centent = @"";
            interface =@"";
            property = @"";
            
        }
        
        [self createOBJCFile:string superString:superString  importModel:importModel content:centent interface:interface property:property];
        
    }
    
    
}




-(void)createOBJCFile:(NSString *)fileName superString:(NSString *)superString  importModel:(NSString *)importModel content:(NSString *)content interface:(NSString *)interface property:(NSString *)property;
{
    // 替换文件名字

    self.modelHeaderFileString = self.nodeModelStringsPlist[@"modelHeaderFileString"];
    self.modelMFileString      = self.nodeModelStringsPlist[@"modelMFileString"];
    
    self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[ModelName-WaitForReplaced]"
                                                                                       withString:fileName];
    
    self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[super-WaitForReplaced]"
                                                                                       withString:superString];
    
    self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[BaseModel-WaitForReplaced]"
                                                                                       withString:importModel];
    
    self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[Interface-WaitForReplaced]"
                                                                                       withString:interface];
    
    NSLog(@"self.modelHeaderFileString === %@  super ==== %@",self.modelHeaderFileString,superString);
    
    
    self.modelMFileString      = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[ModelName-WaitForReplaced]"
                                                                                  withString:fileName];
    
    self.modelMFileString      = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[super-WaitForReplaced]"
                                                                                  withString:superString];
    
    
    self.modelMFileString      = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[BaseModel-WaitForReplaced]"
                                                                                  withString:importModel];

    
    self.modelMFileString  = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[ImplementContent-WaitForReplaced]"
                                                      withString:content];

    self.modelMFileString  = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[Property-WaitForReplaced]"
                                                                              withString:property];
    

    [self.modelHeaderFileString writeToFile:[self filePathWithFileName:[fileName stringByAppendingString:@".h"]]
                                 atomically:YES
                                   encoding:NSUTF8StringEncoding
                                      error:nil];

    [self.modelMFileString writeToFile:[self filePathWithFileName:[fileName stringByAppendingString:@".m"]]
                            atomically:YES
                              encoding:NSUTF8StringEncoding
                                 error:nil];

}

- (NSString *)filePathWithFileName:(NSString *)name {
    
    NSString *string = [NSString stringWithFormat:@"%@/%@",self.filePah,name];

    return string;
}







@end
